package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.bo.ZhcaInformationAddBo;
import com.ruoyi.system.bo.ZhcaInformationEditBo;
import com.ruoyi.system.bo.ZhcaInformationQueryBo;
import com.ruoyi.system.service.IWechatService;
import com.ruoyi.system.service.IZhcaInformationService;
import com.ruoyi.system.vo.ZhcaInformationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 资讯管理Controller
 *
 * @author ruoyi
 * @date 2021-07-27
 */
@Validated
@Api(value = "资讯管理控制器", tags = {"资讯管理管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/information")
@Slf4j
public class ZhcaInformationController extends BaseController {

	private final IZhcaInformationService iZhcaInformationService;
	private final IWechatService iWechatService;

	/**
	 * 查询资讯管理列表
	 */
	@ApiOperation("获取微信二维码")
	//@PreAuthorize("@ss.hasPermi('system:information:list')")
	@GetMapping("/wx-qrCode")
	public void getWxQrCode(HttpServletResponse response) {
		byte[] bytes = iWechatService.getLoginQrCode();
		response.setContentType("image/jpg");
		try (ServletOutputStream out = response.getOutputStream()) {
			out.write(bytes);
		} catch (IOException e) {
			log.error("获取微信二维码失败", e);
		}
	}


	/**
	 * 查询资讯管理列表
	 */
	@ApiOperation("查询资讯管理列表")
	//@PreAuthorize("@ss.hasPermi('system:information:list')")
	@GetMapping("/list")
	public TableDataInfo<ZhcaInformationVo> list(@Validated ZhcaInformationQueryBo bo) {
		return iZhcaInformationService.queryPageList(bo);
	}

	/**
	 * 导出资讯管理列表
	 */
	@ApiOperation("导出资讯管理列表")
	@PreAuthorize("@ss.hasPermi('system:information:export')")
	@Log(title = "资讯管理", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult<ZhcaInformationVo> export(@Validated ZhcaInformationQueryBo bo) {
		List<ZhcaInformationVo> list = iZhcaInformationService.queryList(bo);
		ExcelUtil<ZhcaInformationVo> util = new ExcelUtil<ZhcaInformationVo>(ZhcaInformationVo.class);
		return util.exportExcel(list, "资讯管理");
	}

	/**
	 * 获取资讯管理详细信息
	 */
	@ApiOperation("获取资讯管理详细信息")
	@PreAuthorize("@ss.hasPermi('system:information:query')")
	@GetMapping("/{id}")
	public AjaxResult<ZhcaInformationVo> getInfo(@NotNull(message = "主键不能为空")
	                                             @PathVariable("id") Long id) {
		return AjaxResult.success(iZhcaInformationService.queryById(id));
	}

	/**
	 * 新增资讯管理
	 */
	@ApiOperation("新增资讯管理")
	@PreAuthorize("@ss.hasPermi('system:information:add')")
	@Log(title = "资讯管理", businessType = BusinessType.INSERT)
	@RepeatSubmit
	@PostMapping()
	public AjaxResult<Void> add(@Validated @RequestBody ZhcaInformationAddBo bo) {
		return toAjax(iZhcaInformationService.insertByAddBo(bo) ? 1 : 0);
	}

	/**
	 * 修改资讯管理
	 */
	@ApiOperation("修改资讯管理")
	@PreAuthorize("@ss.hasPermi('system:information:edit')")
	@Log(title = "资讯管理", businessType = BusinessType.UPDATE)
	@RepeatSubmit
	@PutMapping()
	public AjaxResult<Void> edit(@Validated @RequestBody ZhcaInformationEditBo bo) {
		return toAjax(iZhcaInformationService.updateByEditBo(bo) ? 1 : 0);
	}

	/**
	 * 删除资讯管理
	 */
	@ApiOperation("删除资讯管理")
	@PreAuthorize("@ss.hasPermi('system:information:remove')")
	@Log(title = "资讯管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
	                               @PathVariable Long[] ids) {
		return toAjax(iZhcaInformationService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
	}
}
